package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.*;
import com.c.mapper.*;
import com.c.service.MemberFileReportService;
import com.c.service.MemberService;
import com.c.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author wangxiangli
 * @create 2022-12-30-20:42
 */
@Service
@Transactional
public class MemberFileReportServiceImpl extends ServiceImpl<MemberFileReportMapper, MemberFileReport> implements MemberFileReportService {

   @Autowired
   private UserMapper userMapper;


   @Autowired
   private PopulationClassificationMapper populationClassificationMapper;

    @Autowired
    private TodayInformationMapper todayInformationMapper;
    @Autowired
    private TotalInformationMapper totalInformationMapper;


    /**
     * 根据页码查找会员
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPageManger(QueryPageBean queryPageBean) {
        IPage<MemberFileReport> pageMemberFileReport=new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<MemberFileReport> wrapper=new QueryWrapper();
        String queryString = queryPageBean.getQueryString();
        //判断条件值是否为空，如果不为空拼接条件
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryString = queryPageBean.getQueryString().trim();// 去除查询条件首尾空格
            wrapper.like("name",queryString);
            wrapper.or().like("fileNumber",queryString);
            wrapper.or().like("phoneNumber",queryString);
        }
        IPage page1 = baseMapper.selectPage(pageMemberFileReport, wrapper);
        return new PageResult(page1.getTotal(),page1.getRecords());
    }

    /**
     * 根据页码查找会员
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean,User user) {
        IPage<MemberFileReport> pageMemberFileReport=new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        QueryWrapper<MemberFileReport> wrapper=new QueryWrapper();
        String queryString = queryPageBean.getQueryString();
        //判断条件值是否为空，如果不为空拼接条件
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryString = queryPageBean.getQueryString().trim();// 去除查询条件首尾空格
            wrapper.like("name",queryString);
            wrapper.or().like("fileNumber",queryString);
            wrapper.or().like("phoneNumber",queryString);
        }
        if (queryPageBean.getQueryString() == null || queryPageBean.getQueryString().length() == 0) {
            wrapper.eq("doctor_id",user.getId());
        }
        IPage page1 = baseMapper.selectPage(pageMemberFileReport, wrapper);
        return new PageResult(page1.getTotal(),page1.getRecords());
    }

    /**
     * 上传体检报告到数据库
     *
     * @param memberFileReport
     */
    @Override
    public Result uploadFileReport(MemberFileReport memberFileReport) throws Exception {
//        if ("1".equals(memberFileReport.getFilestatus())) {
//            return new Result(false, MessageConstant.ERROR_UPLOAD);
//        }
        memberFileReport.setFilestatus("1");
        baseMapper.updateById(memberFileReport);

        this.addpopulationClassification(memberFileReport);
        //更新评估人数
        QueryWrapper<TodayInformation> todayInformationQueryWrapper=new QueryWrapper<>();
        String date2String = DateUtils.parseDate2String(new Date());
        todayInformationQueryWrapper.eq("todaydate",date2String);
        TodayInformation todayInformation = todayInformationMapper.selectOne(todayInformationQueryWrapper);
        List<TotalInformation> totalInformations = totalInformationMapper.selectList(null);
        if(Objects.isNull(todayInformation)){
            TodayInformation todayInformation1=new TodayInformation();
            todayInformation1.setTodayordernumber(0);
            todayInformation1.setTodayestimatenumber(1);
            todayInformation1.setTodayintervenenumber(0);
            todayInformation1.setTodaycommentnumber(0);
            Date newdate = DateUtils.parseString2Date(DateUtils.parseDate2String(new Date()));
            todayInformation1.setTodayDate(newdate);
            todayInformationMapper.insert(todayInformation1);
            TotalInformation totalInformation = totalInformations.get(0);
            totalInformation.setTotalestimatenumber(totalInformation.getTotalestimatenumber()+1);
            totalInformationMapper.updateById(totalInformation);
        }else{
            todayInformation.setTodayestimatenumber(todayInformation.getTodayestimatenumber()+1);
            todayInformationMapper.updateById(todayInformation);
            TotalInformation totalInformation = totalInformations.get(0);
            totalInformation.setTotalestimatenumber(totalInformation.getTotalestimatenumber()+1);
            totalInformationMapper.updateById(totalInformation);
        }
        //将文件名称保存到Redis集合中
//        String fileName = memberFileReport.getFileurl();
//        jedisPool.getResource().sadd(RedisConstant.MEMBER_FILE_DB_RESOURCES, fileName);
        return new Result(true, MessageConstant.ADD_MEMBERFILEREPORT_SUCCESS);

    }

    public void addpopulationClassification(MemberFileReport memberFileReport){
        QueryWrapper<PopulationClassification> wrapper=new QueryWrapper<>();
        wrapper.eq("checkNumber",memberFileReport.getCheckNumber());
        PopulationClassification populationClassification1 = populationClassificationMapper.selectOne(wrapper);
        if(Objects.isNull(populationClassification1)){
            PopulationClassification populationClassification=new PopulationClassification();
            populationClassification.setCheckNumber(memberFileReport.getCheckNumber());
            populationClassification.setMemberId(memberFileReport.getMemberId());
            populationClassification.setMemberName(memberFileReport.getName());
            populationClassification.setSex(memberFileReport.getSex());
            populationClassification.setAge(memberFileReport.getAge());
            populationClassification.setDoctorId(memberFileReport.getDoctorId());
            User doctorUser = userMapper.selectById(memberFileReport.getDoctorId());
            populationClassification.setDoctorName(doctorUser.getRealname());
            populationClassification.setStatus("0");
            populationClassification.setCreateId(memberFileReport.getDoctorId());
            populationClassification.setUpdateId(memberFileReport.getDoctorId());
            populationClassificationMapper.insert(populationClassification);
        }else{
            populationClassification1.setAge(memberFileReport.getAge());
            populationClassification1.setCreateId(memberFileReport.getDoctorId());
            populationClassification1.setUpdateId(memberFileReport.getDoctorId());
            populationClassificationMapper.updateById(populationClassification1);
        }


    }
    /**
     * 删除体检报告
     *
     * @param id
     * @return
     */
    @Override
    public void deleteFileReport(Integer id) {
        MemberFileReport memberFileReport = baseMapper.selectById(id);
        String delFileurl = memberFileReport.getFileurl();
        AliyunUtils.deleteFileFromAliyun(delFileurl);
//
//        //删除Redis集合中数据
//        jedisPool.getResource().srem(RedisConstant.MEMBER_FILE_DB_RESOURCES, delFileurl);

        baseMapper.deleteFileReport(id);
    }
}
